package ma.view.gui.parameterPanels;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;

import ma.view.gui.auxillary.NumericalTextField;
import net.miginfocom.swing.MigLayout;

/**
 * An {@code InputPanel} shows input parameter panels. It shows
 * the parameter's name, abbreviation, it's unit and a text field where the
 * user can input a value. 
 * @author Oliver Eckstein
 *
 */
public abstract class InputPanel
    extends ParameterPanel {
    
    private static final long serialVersionUID = 1L;
    protected int geometryCount = 0;
    
    
    /*
     * Map containing all created text fields. They get identified by their
     * parameter's abbreviation.
     */    
    protected Map<String, NumericalTextField> textFields =
            new HashMap<String, NumericalTextField>();
    
    /*
     * Map containing all created text fields that are connected to geometry
     * sets. They get identified by their parameter's abbreviation.
     */    
    protected Map<Integer, ArrayList<NumericalTextField>> textFieldsMap =
            new HashMap<Integer, ArrayList<NumericalTextField>>();

    /**
     * To create a new instance of {@code InputParameterPanelsPanel}.
     * 
     */
    public InputPanel(
            String logoPath,
            String panelTitle) {               
        super(logoPath, panelTitle);                
    }    
    
    @Override
    /*
     * (non-Javadoc)
     * @see ma.view.gui.ParameterPanelsPanel#addParameterPanel(java.lang.String[])
     */
    public void addParameterPanel(String[] parameterInformation) {
        
        parametersPanel.add(new JLabel(parameterInformation[0]));            
        parametersPanel.add(new JLabel(parameterInformation[2]));
        parametersPanel.add(new JLabel(parameterInformation[3]));
        
        NumericalTextField valueTF = new NumericalTextField(8);
        valueTF.setText(parameterInformation[4]);
        valueTF.setIdentification(parameterInformation[1]);
        parametersPanel.add(valueTF);         
        textFields.put(parameterInformation[1], valueTF);        
    }
    
    @Override
    /*
     * (non-Javadoc)
     * @see ma.view.gui.ParameterPanelsPanel#addParameterPanel(java.util.List)
     */
    public void addParameterPanel(
            List<String[]> parametersInformation, String borderTitle) {        
        // The text fields of a geometry set.
        ArrayList<NumericalTextField> txtFieldsList =
                new ArrayList<NumericalTextField>();
        ArrayList<JPanel> panelsList =
                new ArrayList<JPanel>();
        
        JPanel parameterPanel = new JPanel(new MigLayout("wrap 4"));
        parameterPanel.setBorder(BorderFactory
                .createTitledBorder(borderTitle));
        
        //Iterate through every parameter of this set.
        for(String[] parameter : parametersInformation) {                            
            
            parameterPanel.add(new JLabel(parameter[0]));
            parameterPanel.add(new JLabel(parameter[2]));
            parameterPanel.add(new JLabel(parameter[3]));                
            
            NumericalTextField valueTF = new NumericalTextField(8);
            valueTF.setText(parameter[4]);
            valueTF.setIdentification(parameter[1]);
            parameterPanel.add(valueTF);
            panelsList.add(parameterPanel);           
            txtFieldsList.add(valueTF);
        }
        parametersPanel.add(parameterPanel);
        textFieldsMap.put(geometryCount, txtFieldsList);
        parameterPanelsMap.put(geometryCount, panelsList);
        geometryCount++;
    }
    
    /**
     * To get all text fields of all parameter panels of this panel that are
     * not connected to a geometry set.
     * @return all text fields of this panel that are not connected to a
     * geometry set.
     */
    public Map<String, NumericalTextField> getNonGeometricTextFields() {
        return textFields;
    }
    
    /**
     * To get all text fields of this panel that are connected to a geometry
     * set.
     * @return all text fields of this panel that are connected to a geometry
     * set.
     */
    public Map<Integer, ArrayList<NumericalTextField>>
        getGeometricValueTextFields() {
        return textFieldsMap;
    }
    
    /**
     * To get the number of geometry sets shown on this panel.
     * @return number of geometry sets.
     */
    public int getGeometryCount() {
        return geometryCount;
    }
}
